import email
import imp
from pydoc import describe
from django.test import TestCase
from django.contrib.auth.models import User
from shopping.models import Cart
from catalogue.models import Catalogue, Foods
from shopping.serializers import CartSerializer
from ordering.models import Order
from ordering.serializers import OrderSerializer
# Create your tests here.


class TT(TestCase):
    def setUp(self):
        user = User.objects.create(
            id=1,
            username='kali',
            password='zlg126...',
            email='397503810@qq.com',
            is_staff=1,
            is_active=1,
            is_superuser=1,
        )
        Catalogue.objects.create(
            id=1,
            tags='干果类',
            author_id=1,
        )
        Catalogue.objects.create(
            id=2,
            tags='葡萄',
            author_id=1,
        )
        Foods.objects.create(
            name='红提子',
            describe='是个帅哥',
            sales=34645,
            price=24,
            images='/media/sdgs.jpg',
            catalogue_id=2,
        )
        Foods.objects.create(
            name='巴大木',
            describe='是的感受到',
            sales=1234,
            price=13,
            images='/media/a.jpg',
            catalogue_id=1,
        )
        Foods.objects.create(
            name='核桃',
            describe='是的感受到',
            sales=12345,
            price=18,
            images='/media/b.jpg',
            catalogue_id=1,
        )
        Cart.objects.create(
            count = 2,
            fid_id = 1,
            user_id = 1,
        )
        Cart.objects.create(
            count = 5,
            fid_id = 2,
            user_id = 1,
        )
        Order.objects.create(
            user_id=1,
            oid = '20220213110801',
            count=2,
            deliver='京东',
            address='河南省濮阳市',
            foods_id=1,
        )
        Order.objects.create(
            user_id=1,
            oid = '20220213110801',
            count=5,
            deliver='京东',
            address='河南省濮阳市',
            foods_id=2,
        )
        Order.objects.create(
            user_id=1,
            oid = '20220213110809',
            count=7,
            deliver='圆通',
            address='河南省濮阳市',
            foods_id=3,
        )

    def test_serializer(self):
        print('test la -->')
        user = User.objects.get(id=1)
        # print('cata->', Catalogue.objects.get(id=1).types.all())
        # print('here->', user)
        # u = user.carts.all()
        # print('u->', u)
        # cs = CartSerializer(u, many=True)
        # print('res->', cs.data)
        # if cs.is_valid():
        #     print('data->', cs.validated_data)
        # else:
        #     print('error->', cs.errors)
        u = user.orders.all().values_list("oid").distinct()
        print('u->', u)
        res = []
        for i in u:
            print("i --> ", i[0], type(i[0]))
            order = Order.objects.filter(oid=i[0]).all()
            print("order --> ", order)
            for j in order:
                print(j.foods.name)
            # if order:
            #     serializer = OrderSerializer(order)
            #     res.append(serializer.data)
        print("res --> ", res)
        # os = OrderSerializer(u, many=True)
        # print('os ---->', os.data)

